//
//  AXFGuideView.m
//  AXFEightGroup
//
//  Created by ya on 12/28/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFGuideView.h"
#import "AXFDataModel.h"

@interface AXFGuideView ()
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *overButton;
@end

@implementation AXFGuideView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {

     self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:self.imageView];

    [_ModelMgr adverts:^(NSArray<AXFDataModel *> * _Nullable models) {
        [self.imageView sd_setImageWithURL:[models[0].img_name sj_url]];

    }];


    self.overButton = [UIButton new];
    [self addSubview:self.overButton];
    [self.overButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self).offset(-25.0);
        make.top.equalTo(self).offset(25.0);
    }];
    [self.overButton setTitle:@"跳过( )" forState:UIControlStateNormal];
    [_overButton addTarget:self
                    action:@selector(actionClickedOverButton)
          forControlEvents:UIControlEventTouchUpInside];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(ttt) userInfo:nil repeats:YES];
}

- (void)ttt {
    static NSInteger index = 5;
    index --;
    [self.overButton setTitle:[NSString stringWithFormat:@"跳过(%zd)", index] forState:UIControlStateNormal];
    if (index == 0) {
        [self actionClickedOverButton];
    }
}

- (void)actionClickedOverButton {
    [self.timer invalidate];

    CABasicAnimation *anima1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    anima1.fromValue = [NSNumber numberWithFloat:1.0];
    anima1.toValue   = [NSNumber numberWithFloat:0.2];
    anima1.duration  = 0.5;
    [self.layer addAnimation:anima1 forKey:@"opacityAnimation"];

    CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    anima2.toValue   = [NSNumber numberWithFloat:2.0];
    anima2.duration  = 0.5;
    [self.layer addAnimation:anima2 forKey:@"scaleAnimation"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}




@end
